package RPPM.dao.impl;

import RPPM.dao.StuDao;
import RPPM.domain.*;
import RPPM.util.JDBCUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class StuDaoImpl implements StuDao {
    private JdbcTemplate jdbcTemplate =new JdbcTemplate(JDBCUtils.getDataSource());

    /**
     * findCourse：查询当前学生课程信息
     * @param stu_id
     * @return
     */
    @Override
    public List<Map<String, Object>> findCourses(int stu_id) {
        System.out.println("findCourse");
        String sql ="SELECT course.Cou_id, course.Cou_name, teacher.Tea_name, course.Cou_time FROM course JOIN teacher ON course.Tea_id = teacher.Tea_id JOIN course_student ON course.Cou_id = course_student.Cou_id JOIN student ON course_student.Stu_id = student.Stu_id WHERE student.Stu_id = ?";
        try {
            List<Map<String, Object>> courses;
            courses = jdbcTemplate.queryForList(sql, stu_id);
            return courses;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public Student findStudentById(Student tobechecked) {
        //1.定义sql
        String sql ="select * from student where Stu_id = ? ";
        //2.执行查询
        Student stu= null;

        System.out.println(tobechecked.getStu_id()+ " " + tobechecked.getStu_password());
        try {
            stu = jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<Student>(Student.class),tobechecked.getStu_id());
        } catch (Exception e) {
            //e.printStackTrace();
            stu = null;
            return stu;
        }
        return stu;
    }


    @Override
    public List<Map<String, Object>> findCoursesByname(int stuid, String searchText) {
        String sql = "SELECT course.Cou_id, course.Cou_name, teacher.Tea_name, course.Cou_time FROM course JOIN teacher ON course.Tea_id = teacher.Tea_id JOIN course_student ON course.Cou_id = course_student.Cou_id JOIN student ON course_student.Stu_id = student.Stu_id WHERE student.Stu_id = ? and course_student.Cou_name like ?";
        try{
            List<Map<String, Object>> courses;
            courses = jdbcTemplate.queryForList(sql, stuid, "%"+searchText+"%");
            return courses;
        } catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public List<Map<String, Object>> findCoursesNotPaid(int stu_id) {
        String sql = "select course.Cou_id, course.Cou_name, course.Cou_time, course.Cou_teacher, course.Cou_payment from course, course_student where course.Cou_id = course_student.Cou_id  and course_student.Cou_payment = 0 and course_student.Stu_id = ?";
        try{
            List<Map<String, Object>> courses;
            courses = jdbcTemplate.queryForList(sql, stu_id);
            return courses;
        } catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public List<Map<String, Object>> searchByCouNameEX(int stu_id, String str) {
        String sql = "select course.Cou_id, course.Cou_name, course.Cou_time, course.Cou_teacher, course.Cou_payment from course, course_student where course.Cou_id = course_student.Cou_id and course_student.Cou_payment = 0 and course_student.Stu_id = ? and course.Cou_name like ?";
        try{
            List<Map<String, Object>> courses;
            courses = jdbcTemplate.queryForList(sql, stu_id, "%"+str+"%");
            return courses;
        } catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public List<Map<String, Object>> findCoursesNotSign(int stu_id) {
        String sql = "select course_student.Cou_id, course_student.Cou_name, course.Cou_time, course.Cou_teacher from course_student, course where course_student.Stu_id = ? and course_student.Cou_id = course.Cou_id and course_student.Stu_sign = 0";
        try{
            List<Map<String, Object>> courses;
            courses = jdbcTemplate.queryForList(sql, stu_id);
            return courses;
        } catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public List<Map<String, Object>> findCoursesNotAdvice(int stu_id) {
        String sql = "select course_student.Cou_id, course_student.Cou_name, course.Cou_time, course.Cou_teacher from course_student, course where course_student.Stu_id = ? and course_student.Cou_id = course.Cou_id and course_student.Cou_advice = '0'";
        try{
            List<Map<String, Object>> courses;
            courses = jdbcTemplate.queryForList(sql, stu_id);
            return courses;
        } catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public boolean saveCourseSign(int stu_id, Map<String, Object> pay) {
        String sql ="update course_student set Stu_sign = 1 where Stu_id = ? and Cou_name = ?";

        int count=jdbcTemplate.update(sql, stu_id, pay.get("Cou_name"));

        return (count == 1)?true:false;
    }

    @Override
    public boolean saveCourseAdvice(int stu_id, Map<String, Object> advice, String feedback) {
        String sql ="update course_student set Cou_advice = ? where Stu_id = ? and Cou_name = ?";

        int count=jdbcTemplate.update(sql, feedback, stu_id, advice.get("Cou_name"));

        return (count == 1)?true:false;
    }



    @Override
    public boolean savePayment(Payment payment,int stu_id) {
        String sql = "insert into payment(Stu_id,Pay_money,Pay_Cou,Pay_time) values (?,?,?,?)";
        String sql1 = "update course_student set Cou_payment = 1 where Stu_id = ? and Cou_name = ? and Cou_payment = 0";
        try{
            Date date = new Date();
            jdbcTemplate.update(sql,
                    stu_id,
                    payment.getPay_money(),
                    payment.getPay_Cou(),
                    new Timestamp(date.getTime())
            );

            jdbcTemplate.update(sql1, stu_id, payment.getPay_Cou());


            return true;
        } catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }


}
